package com.cnpc.rightslogan.controller;

import com.alibaba.fastjson.JSON;
import com.cnpc.rightslogan.entity.Card;
import com.cnpc.rightslogan.service.ICardService;
import com.cnpc.rightslogan.utils.common.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/9/13.
 */
@RestController
@RequestMapping("card")
@Api("CardController相关的api")
@Slf4j
public class CardController extends BaseController {

    @Resource
    private ICardService cardServiceImpl;

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @ApiOperation(value = "获取所有卡牌", notes = "获取所有卡牌")
    @ResponseBody
    @GetMapping("/list")
    public List<Card> getCardList() {
        log.info("获取所有卡牌");
        return getAllCardList();
    }

    @ApiOperation(value = "抽牌", notes = "抽牌")
    @ResponseBody
    @GetMapping("/attackList/getUnusedCard/{roomNum}")
    public List<Card> getCard(@PathVariable String roomNum) {
        log.info("抽牌");
        //获取key
        String key = roomNum + "unusedCard";
        //判断是不是第一次抽牌
        if (!stringRedisTemplate.hasKey(key)) {
            drawCardAgain(key);
        }
        //从自己牌组中随机抽取3张牌
        List<String> list = stringRedisTemplate.opsForSet().randomMembers(key, 3);
        List<Card> cardOnHandList = new ArrayList<>();
        for (String card : list) {
            cardOnHandList.add(randomMembersFormat(card));
        }
        return cardOnHandList;
    }

    //处理随机获取到的卡牌数据
    public Card randomMembersFormat(String card) {
        card = card.replace("Card(", "")
                .replace(")", "").replaceAll(" ", "");
        String[] cardArray = card.split(",");
        Map map = new HashMap();
        for (String s : cardArray) {
            String[] sArray = s.split("=");
            map.put(sArray[0], sArray[1]);
        }
        Card oneCard = new Card();
        oneCard.setCardId(Integer.parseInt(map.get("cardId").toString()));
        oneCard.setCardName(map.get("cardName").toString());
        oneCard.setCardRemark(map.get("cardRemark").toString());
        oneCard.setCardType(Integer.parseInt(map.get("cardType").toString()));
        oneCard.setPhysicalDamage(Integer.parseInt(map.get("physicalDamage").toString()));
        return oneCard;
    }

    //重新抽取卡牌    已有卡牌重新回到牌组  抽光卡牌时调用
    public void drawCardAgain(String key) {
        List<Card> cardList = getAllCardList();
        for (Card card : cardList) {
            stringRedisTemplate.opsForSet().add(key, card.toString());
        }
    }

    //获取所有卡牌
    public List<Card> getAllCardList() {
        List<Card> cardList;
        String cardJsonList;
        //如果Redis缓存有数据,从缓存中获取数据
        if (stringRedisTemplate.hasKey("card")) {
            log.info("卡牌数据通过缓存获取.");
            cardJsonList = stringRedisTemplate.opsForValue().get("card");
            cardList = JSON.parseArray(cardJsonList, Card.class);
            return cardList;
        } else {    //如果Redis缓存没有数据,从数据库中获取数据   但是这种情况会导致缓存击穿   容易导致雪崩
            log.warn("卡牌数据通过数据库获取,此消息不应多次重复出现.");
            cardList = cardServiceImpl.getCardList();
            cardJsonList = JSON.toJSONString(cardList);
            stringRedisTemplate.opsForValue().set("card", cardJsonList);
            return cardList;
        }
    }
}